﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using Time.IBLL;
using Time.BLL;

namespace MesDock
{
    public partial class SelectForm : Form
    {
        public static string messageInfo;
        public SelectForm()
        {
            InitializeComponent();
        }
        IEqAlarm_BLL objIEqAlarm_BLL = new EqAlarm_BLL();
        private void Form_Load(object sender, EventArgs e)
        {
            #region    //定义系统状态栏 系统时间
            this.toolStripStatusLabel3.Text = "登录时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            this.toolStripStatusLabel3.Text = "系统当前时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            this.timer1.Interval = 1000;
            this.timer1.Start();
            #endregion
            //设置导出文件可选格式
            saveFileDialog1.Filter = "Excle文件(*.xlsx)|*.xlsx";

            #region  定义DGW控件查询的数据居中显示
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();//定义单元格数据居中显示
            dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;      //定义单元格数据居中显示
            this.dataGridView1.DefaultCellStyle = dataGridViewCellStyle1; //定义单元格数据居中显示
            dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;//定义DataGriwView1列标题居中显示
            #endregion

            #region 定义查询条件默认选项
            ///为查询选项下定义几个默认值         
            cbDeviceNo.Items.Add("J02GZXT100001");
            cbDeviceNo.Items.Add("J02GZXT100002");
            cbDeviceNo.Items.Add("J02GZXT100003");
            cbCarNo.Items.Add("HK220120170630145555");
            cbCarNo.Items.Add("HK220120170630145232");
            cbCarNo.Items.Add("HK220120170630145523");
            cbCarNo.Items.Add("HK220120170630145540");
            cbBarcode.Items.Add("M2117133010326");
            cbBarcode.Items.Add("M2117133010327");
            cbBarcode.Items.Add("M2117133010328");
            cbBarcode.Items.Add("M2117133010329");
            #endregion
        }

        private void timer1_Tick(object sender, EventArgs e)  //定义系统状态栏 系统时间
        {
            this.toolStripStatusLabel3.Text = "系统当前时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        private void btnDesc_Click(object sender, EventArgs e) //查询功能使用说明
        {
            //new ExcelOperate().WriteExcel((DataTable)dataGridView1.DataSource);
            MessageBox.Show("请输入或选择查询条件,完成数据查询操作.\r\n然后再进行导出操作.", "系统提示");
        }

        #region 定义数据查询功能
        private void btnQuery_Click(object sender, EventArgs e)
        {
            messageInfo = "正在查询，请稍候...";
            messageForm messageFrm = new messageForm();
            //提示窗口居中显示
            messageFrm.StartPosition = FormStartPosition.CenterScreen;
            messageFrm.Show();
            Application.DoEvents();


            //string sql = "select pro.扫码时间,pro.设备编号,pro.电池二维码,pro.小车流水号,etp.采集时间,etp.工位,etp.温度测量值PV1,"+
            //    "etp.温度测量值PV2,etp.真空度PV from HK_EqProductInfo pro,HK_EqTechnologyParameterInfo etp where pro.小车流水号=etp.小车流水号";
            string sqlStr = "select  扫码时间, 设备编号, 电池二维码, 小车流水号, 测试结果, 电池代码 from HK_EqProductInfo  ";
            string fWhere = " where 1=1 ";
            int i = 0;
            if (!string.IsNullOrEmpty(cbDeviceNo.Text))
            {
                fWhere += " and 设备编号 = '" + cbDeviceNo.Text + "'";
            }
            if (!string.IsNullOrEmpty(cbBarcode.Text))
            {
                fWhere += " and 电池二维码 = '" + cbBarcode.Text + "'";
            }
            if (!string.IsNullOrEmpty(cbCarNo.Text))
            {
                fWhere += " and 小车流水号 = '" + cbCarNo.Text + "'";
            }

            if (!string.IsNullOrEmpty(startDate.Text))
            {
                string st = startDate.Text;
                if (!string.IsNullOrEmpty(startTime.Text))
                {
                    st += " " + startTime.Text;
                }
                else
                {
                    st += " " + " 00:00:00 ";
                }
                fWhere += " and 扫码时间 >= '" + st + "'";
            }
            if (!string.IsNullOrEmpty(endDate.Text))
            {
                string st = endDate.Text;
                if (!string.IsNullOrEmpty(endTime.Text))
                {
                    st += " " + endTime.Text;
                }
                else
                {
                    string Hour = DateTime.Now.Hour.ToString();// 获取小时
                    string Minute = DateTime.Now.Minute.ToString();// 获取分钟
                  string  Second = DateTime.Now.Second.ToString();// 获取秒
                    st += " " + Hour + ":" + Minute + ":" + Second;
                }
                fWhere += " and 扫码时间 <= '" + st + "'";
            }
            sqlStr += fWhere;
            dataGridView1.DataSource = objIEqAlarm_BLL.getData(sqlStr);

            messageFrm.Close();

        }
        #endregion

        #region 定义退出按钮
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 定义数据导出功能
        private void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                DataGridView dataGrid = null;
                if (this.ActiveControl != null)
                {
                    switch (this.ActiveControl.Name)
                    {
                        case "btnExport":
                            dataGrid = this.dataGridView1;
                            break;
                        case "btnProcessExport":
                            dataGrid = this.dataGridView2;
                            break;
                        case "btnRepeatExport":
                            dataGrid = this.dataGridView3;
                            break;
                    }
                }
                if (dataGrid.RowCount == 1)
                {
                    MessageBox.Show("没有可以导出的数据！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                //打开窗口，选择保存地址、填写文件名
                saveFileDialog1.ShowDialog();

                messageInfo = "数据正在导出，请稍候...";
                messageForm msgFrm = new messageForm();
                msgFrm.StartPosition = FormStartPosition.CenterScreen;
                msgFrm.Show();
                //msgFrm.ShowDialog();
                Application.DoEvents();
                bool result = (new ExcelOperate()).DataExport((DataTable)dataGrid.DataSource, saveFileDialog1.FileName);
                msgFrm.Close();
                if (result)
                {
                    MessageBox.Show("导出成功!", "系统提示");
                }
                else
                {
                    MessageBox.Show("数据导出失败,请确保电脑已经安装Excel", "系统提示");

                }
            }
            catch (Exception)
            {
                throw;
            }

        }
        #endregion

        #region datagrid添加序号
        private void dataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            try
            {
                //电池二维码
                System.Drawing.Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y,
                    dataGridView1.RowHeadersWidth - 4, e.RowBounds.Height);
                TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dataGridView1.RowHeadersDefaultCellStyle.Font,
                    rectangle, dataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine);

                //生产过程
                System.Drawing.Rectangle rectangle2 = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y,
                        dataGridView2.RowHeadersWidth - 4, e.RowBounds.Height);
                TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dataGridView2.RowHeadersDefaultCellStyle.Font,
                    rectangle, dataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine);

                //重扫电池
                System.Drawing.Rectangle rectangle3 = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y,
                        dataGridView3.RowHeadersWidth - 4, e.RowBounds.Height);
                TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dataGridView3.RowHeadersDefaultCellStyle.Font,
                    rectangle, dataGridView3.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine);
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
        /// <summary>
        /// 工位查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnProcessQuery_Click(object sender, EventArgs e)
        {
            messageInfo = "正在查询，请稍候...";
            messageForm messageFrm = new messageForm();
            //提示窗口居中显示
            messageFrm.StartPosition = FormStartPosition.CenterScreen;
            messageFrm.Show();
            Application.DoEvents();

                                                                                 
            string sqlStr = "select   top(200) * from HK_EqTechnologyParameterInfo where 1=1 ";
            if (!string.IsNullOrEmpty(cbBarcode2.Text))
            {
                sqlStr += " and 电池二维码 = '" + cbBarcode2.Text + "'";
            }
            if (!string.IsNullOrEmpty(cbCarNo2.Text))
            {
                sqlStr += " and 小车流水号 = '" + cbCarNo2.Text + "'";
            }
            if (!string.IsNullOrEmpty(cbStation.Text))
            {
                sqlStr += " and 工位 = '" + cbStation.Text + "'";
            }

            if (!string.IsNullOrEmpty(startDate.Text))
            {
                string st = startDate.Text;
                if (!string.IsNullOrEmpty(startTime.Text))
                {
                    st += " " + startTime.Text;
                }
                sqlStr += " and 采集时间 >= '" + st + "'";
            }
            if (!string.IsNullOrEmpty(endDate.Text))
            {
                string st = endDate.Text;
                if (!string.IsNullOrEmpty(endTime.Text))
                {
                    st += " " + endTime.Text;
                }
                sqlStr += " and 采集时间 <= '" + st + "'";
            }

            dataGridView2.DataSource = objIEqAlarm_BLL.getData(sqlStr).DefaultView;
          
            messageFrm.Close();
        }
        /// <summary>
        /// 重扫查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRPTQuery_Click(object sender, EventArgs e)
        {

            messageInfo = "正在查询，请稍候...";
            messageForm messageFrm = new messageForm();
            //提示窗口居中显示
            messageFrm.StartPosition = FormStartPosition.CenterScreen;
            messageFrm.Show();
            Application.DoEvents();


            string sql = "select cellCode,addDate from RPT_Scan";
            int i = 0;
            if (cbBarcode3.Text == "" && startDate3.Text == "" && endDate3.Text == "")
            {
                dataGridView3.DataSource = objIEqAlarm_BLL.getData(sql);
                dataGridView3.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount - 1;
            }
            else
            {
                if (cbBarcode3.Text != "")
                {
                    sql += i == 0 ? " where cellCode='" + cbBarcode3.Text + "'" : " cellCode='" + cbBarcode3.Text + "'";
                    i++;
                }
                if (startDate3 != null)
                {
                    string sDateTime = (startTime3.Text == "") ? startDate3.Text + " 00:00:00" : startDate3.Text + " " + startTime3.Text;
                    sql += i == 0 ? " where addDate>'" + sDateTime.Trim() + "'" : " and addDate>'" + sDateTime.Trim() + "'";
                    i++;
                }
                if (endDate != null)
                {
                    string eDateTime = (endTime3.Text == "") ? endDate3.Text + " 00:00:00" : endDate3.Text + " " + endTime3.Text;
                    sql += i == 0 ? " where addDate<='" + eDateTime.Trim() + "'" : " and addDate<='" + eDateTime.Trim() + "'";
                    i++;
                }
                dataGridView3.DataSource = objIEqAlarm_BLL.getData(sql);
            }
            messageFrm.Close();
        }

         
    }
}
